Skip to content

feat(webpack): svg loader rules have been updated #12646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

lucabattistini
Copy link

When SVG files are imported (both as ReactComponent or url) and thus inlined in the bundle are also included, in a mistaken way, in the build/static/media and consequence into the asset-manifest.json.

This behaviour is critical when using Service Worker because at the start of the application the browser will send all the pre-cache request for all these SVG files that don't need at all.

This PR resolves #9167

This is a working example created by ejecting the config of a cra template:
my-app.zip

Screenshot 2022-08-09 at 11 05 30

@lucabattistini lucabattistini requested a review from mrmckeb as a code owner August 9, 2022 09:08
@facebook-github-bot
Copy link

Hi @lucabattistini!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@lucabattistini
Copy link
Author

Hi @mrmckeb, following the svgr/webpack documentation (https://react-svgr.com/docs/webpack/) the 'babel-plugin-named-asset-import' seems to be unnecessary, what do you think? It can be removed?

@artola
Copy link

artola commented Aug 31, 2022

@lucabattistini why not only add the option emitFile: false to the file-loader. This works for me.

            {
              test: /\.svg$/,
              use: [
                {
                  loader: require.resolve('@svgr/webpack'),
                  options: {
                    prettier: false,
                    svgo: false,
                    svgoConfig: {
                      plugins: [{ removeViewBox: false }],
                    },
                    titleProp: true,
                    ref: true,
                  },
                },
                {
                  loader: require.resolve('file-loader'),
                  options: {
                    name: 'static/media/[name].[hash].[ext]',
                    emitFile: false,
                  },
                },
              ],

@lucabattistini
Copy link
Author

@artola because according to the webpack v5 doc, file-loader is deprecated in favor of asset modules

@konradekk
Copy link

excuses for pinging, any progress here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SVG files imported as React components should not end up in build directory
5 participants